﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;
using HotelManagement;
namespace HotelManagement._01.FORM
{
    public partial class F21_Reservation : Form
    {
        Reservation m_Reservation;
        ReservationBUS m_ReservationBUS;
        string RoomID1;
        public F21_Reservation()
        {

            InitializeComponent();
            init();
            loadRoom();
            //loadUser();
            loadGrid();
            if (AssetsGS.instance.RoleUser == "User")
            {
                m_cmd_delete.Enabled = false;
            }


        }
        public void init()
        {
            m_ReservationBUS = new ReservationBUS();
            m_Reservation = new Reservation();
            m_date_reservationDate.Value = DateTime.Now;
        }
        public void loadGrid()
        {
            m_dgv_reservation.DataSource = m_ReservationBUS.ViewReservation();
        }
        public void loadRoom()
        {
            DataTable v_dt = m_ReservationBUS.tblRoom_SelectAll();
            string a = v_dt.Rows[0]["RoomID"].ToString();
            DataTable v_dt1 = new DataTable();
            DataColumn v_column;
            v_column = new DataColumn();
            v_column.DataType = Type.GetType("System.String");
            v_column.ColumnName = "RoomID";
            v_dt1.Columns.Add(v_column);

            v_column = new DataColumn();
            v_column.DataType = Type.GetType("System.String");
            v_column.ColumnName = "RoomName";
            v_dt1.Columns.Add(v_column);
            for (int i = 0; i < v_dt.Rows.Count; i++)
            {
                DataRow row = v_dt1.NewRow();
                row["RoomID"] = v_dt.Rows[i]["RoomID"].ToString();
                row["RoomName"] = v_dt.Rows[i]["RoomID"].ToString()+" - " + v_dt.Rows[i]["Note"].ToString();
                v_dt1.Rows.Add(row);
            }
            m_cbo_room.DisplayMember = "RoomName";
            m_cbo_room.ValueMember = "RoomID";
            m_cbo_room.DataSource = v_dt1;
        }

        public void loadItem(Reservation v)
        {
            m_cbo_room.SelectedValue = v.RoomID;
            //m_cbo_user.SelectedValue = v.UserID;
            m_cbo_customer.DataSource = m_ReservationBUS.SearchCustomer(m_Reservation.CustomerName);
            m_cbo_customer.SelectedValue = v.CustomerID;
            m_date_arrivalDate.Value = v.ArrivalDate;
            m_date_departureDate.Value = v.DepartureDate;
            m_date_reservationDate.Value = v.ReservationDate;
        }

        private void m_dgv_reservation_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            m_Reservation.ReservationID = Convert.ToInt32(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["ReservationID"].Value.ToString());
            m_Reservation.RoomID = m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["RoomID"].Value.ToString();
            RoomID1 = m_Reservation.RoomID;
            m_Reservation.UserID = Convert.ToInt32(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["UserID"].Value.ToString());
            m_Reservation.ReservationDate = Convert.ToDateTime(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["ReservationDate"].Value.ToString());
            m_Reservation.ArrivalDate = Convert.ToDateTime(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["ArrivalDate"].Value.ToString());
            m_Reservation.CustomerID = Convert.ToInt32(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["CustomerID"].Value.ToString());
            m_Reservation.DepartureDate = Convert.ToDateTime(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["DepartureDate"].Value.ToString());
            //m_Reservation.FullName = m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["FullName"].Value.ToString();
            m_Reservation.CustomerName = m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["CustomerName"].Value.ToString();
            m_Reservation.Note = m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["Note"].Value.ToString();
            m_Reservation.DepositCost = Convert.ToInt32(m_dgv_reservation.Rows[m_dgv_reservation.CurrentCell.RowIndex].Cells["DepositCost"].Value.ToString());
            m_cbo_customer.Enabled = false;
            if (m_Reservation.DepositCost > 0)
            {
                m_ckb_DepositCost.Checked = true;
                m_txt_DepositCost.Enabled = true;
                m_txt_DepositCost.Text = m_Reservation.DepositCost.ToString();
            }
            else
            {
                m_ckb_DepositCost.Checked = false;
                m_txt_DepositCost.Enabled = false;
                m_txt_DepositCost.Text = m_Reservation.DepositCost.ToString();
            }

            loadItem(m_Reservation);
            m_cmd_insert.Visible = false;
        }

        private void m_cmd_delete_Click(object sender, EventArgs e)
        {
            if (m_Reservation.ReservationID == 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi nào nào !");
            }
            else
            {
                DialogResult OK;
                OK = MessageBox.Show("Bạn muốn xóa đặt phòng đã chọn ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (OK == DialogResult.Yes)
                {

                    if (m_ReservationBUS.tblReservation_DeleteRow(m_Reservation.ReservationID) && m_ReservationBUS.tblRoom_ChangeStatus(m_Reservation.RoomID, "Empty"))
                    {
                        MessageBox.Show("Thành công !");
                        loadGrid();
                    }
                    else MessageBox.Show("Có lỗi! Không cập nhật được.");
                }
            }
        }

        private void m_cmd_edit_Click(object sender, EventArgs e)
        {
            update();
            if (m_Reservation.ReservationID == 0)
            {
                MessageBox.Show("Bạn chưa chọn bản ghi nào !");
            }
            else if (m_date_arrivalDate.Value.DayOfYear > m_date_departureDate.Value.DayOfYear)
            {
                MessageBox.Show("Ngày rời đi không thể bé hơn ngày đến");
            }
            else if (CheckRevervationUpdate(m_Reservation.RoomID,m_Reservation.ReservationID, m_Reservation.ArrivalDate, m_Reservation.DepartureDate))
            {
                DialogResult OK;
                OK = MessageBox.Show("Bạn muốn sửa bản ghi đã chọn ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (OK == DialogResult.Yes)
                {

                    //bool x1 = m_ReservationBUS.tblRoom_ChangeStatus(m_Reservation.RoomID, "Empty");
                    update();
                    //bool x2 = m_ReservationBUS.tblRoom_ChangeStatus(m_Reservation.RoomID, "Booked");
                    bool x3 = m_ReservationBUS.tblReservation_Update(m_Reservation);
                    if (x3)
                    {
                        RoomStatusBUS a = new RoomStatusBUS();
                        DataTable v = a.ViewOnlyRoom(RoomID1);
                        if (v.Rows.Count == 0)
                        {
                            CheckInBUS m_CheckInBUS=new CheckInBUS();
                            CheckIn m_CheckIn=new CheckIn();
                            m_CheckIn.RoomID = RoomID1;
                            m_CheckInBUS.CheckInRoomUpdateStatus1(m_CheckIn);

                            m_ReservationBUS.tblRoom_ChangeStatus(m_Reservation.RoomID, "Booked");
                            
                        }
                        MessageBox.Show("Thành công !");
                        loadGrid();
                    }
                    else MessageBox.Show("Có lỗi! Không cập nhật được.");
                }
            }
            else
            {
                MessageBox.Show("Phòng này đã có người đặt phòng trong thời gian bạn đã chọn. Đề nghị chọn lại");
            }
        }
        public void update()
        {
            m_Reservation.RoomID = m_cbo_room.SelectedValue.ToString();
            m_Reservation.DepartureDate = m_date_departureDate.Value;
            m_Reservation.ArrivalDate = m_date_arrivalDate.Value;
            m_Reservation.ReservationDate = m_date_reservationDate.Value;
            if (m_txt_DepositCost.Text.Trim() == "")
            {
                m_Reservation.DepositCost = 0;
            }
            else
            {
                if (!CValidate.IsNumber(m_txt_DepositCost.Text))
                {
                    MessageBox.Show("Số tiền không đúng định dạng");
                    return;
                }
                m_Reservation.DepositCost = int.Parse(m_txt_DepositCost.Text.ToString().Trim());
            }
            //m_Reservation.UserID = Convert.ToInt32(m_cbo_user.SelectedValue.ToString());
            if (m_cbo_customer.Text != "")
            {
                m_Reservation.CustomerID = Convert.ToInt32(m_cbo_customer.SelectedValue.ToString());
            }
        }

        private void m_cmd_insert_Click(object sender, EventArgs e)
        {
            update();
            if (m_cbo_customer.Text == "")
            {
                MessageBox.Show("Hãy chọn khách hàng!");
            }
            else if (m_date_arrivalDate.Value.DayOfYear > m_date_departureDate.Value.DayOfYear)
            {
                MessageBox.Show("Ngày rời đi không thể bé hơn ngày đến");
            }
            else if (m_Reservation.ReservationDate.DayOfYear > m_Reservation.ArrivalDate.DayOfYear)
            {
                MessageBox.Show("Ngày đến không thể nhỏ hơn thời gian hiện tại");
            }
            else
            {

                if (CheckRevervation(m_Reservation.RoomID, m_Reservation.ArrivalDate, m_Reservation.DepartureDate))
                {
                    DialogResult OK;
                    OK = MessageBox.Show("Bạn muốn thêm bản ghi ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (OK == DialogResult.Yes)
                    {
                        update();
                        m_Reservation.Note = "0";

                        if (m_ReservationBUS.tblReservation_Insert(m_Reservation) && m_ReservationBUS.tblRoom_ChangeStatus(m_Reservation.RoomID, "Booked"))
                        {
                            
                            MessageBox.Show("Thành công !");
                            loadGrid();
                        }
                        else MessageBox.Show("Có lỗi! Không thêm được.");
                    }
                }
                else
                {
                    MessageBox.Show("Phòng này đã có người đặt phòng trong thời gian bạn đã chọn. Đề nghị chọn lại");
                }
            }

        }

        private void m_cmd_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void m_cmd_search_Click(object sender, EventArgs e)
        {
            m_cbo_customer.DataSource = m_ReservationBUS.SearchCustomer(m_cbo_customer.Text);
        }

        private bool CheckRevervation(string roomID, DateTime arrival, DateTime departure)
        {
            DataTable v_dt;
            v_dt = m_ReservationBUS.CheckRevervation(roomID);
            if (v_dt.Rows.Count > 0)
            {
                DateTime a = DateTime.Parse(v_dt.Rows[v_dt.Rows.Count - 1]["DepartureDate"].ToString());
                if (DateTime.Now < arrival && departure < DateTime.Parse(v_dt.Rows[0]["ArrivalDate"].ToString()))
                {
                    return true;

                }
                if (DateTime.Now < arrival && arrival >= DateTime.Parse(v_dt.Rows[v_dt.Rows.Count - 1]["DepartureDate"].ToString()))
                {
                    return true;

                }


                for (int i = 0; i < v_dt.Rows.Count - 1; i++)
                {
                    if (DateTime.Now < arrival && arrival > DateTime.Parse(v_dt.Rows[i]["DepartureDate"].ToString()) && departure < DateTime.Parse(v_dt.Rows[i + 1]["ArrivalDate"].ToString()))
                    {
                        return true;
                    }
                }
                return false;
            }


            return true;
        }


        private bool CheckRevervationUpdate(string roomID, int ReservationID, DateTime arrival, DateTime departure)
        {
            DataTable v_dt;
            v_dt = m_ReservationBUS.CheckRevervationUpdate(roomID, ReservationID);
            if (v_dt.Rows.Count > 0)
            {
                if (DateTime.Now < arrival && departure < DateTime.Parse(v_dt.Rows[0]["ArrivalDate"].ToString()))
                {
                    return true;

                }
                if (DateTime.Now < arrival && arrival >= DateTime.Parse(v_dt.Rows[v_dt.Rows.Count - 1]["DepartureDate"].ToString()))
                {
                    return true;

                }


                for (int i = 0; i < v_dt.Rows.Count - 1; i++)
                {
                    if (DateTime.Now < arrival && arrival >= DateTime.Parse(v_dt.Rows[i]["DepartureDate"].ToString()) && departure < DateTime.Parse(v_dt.Rows[i ]["ArrivalDate"].ToString()))
                    {
                        return true;
                    }
                }
                return false;
            }


            return true;
        }

        private void m_btn_Status_Click(object sender, EventArgs e)
        {
            F33_RoomStatus v_F33_RoomStatus = new F33_RoomStatus();
            v_F33_RoomStatus.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_cmd_insert.Visible = true;
            m_cbo_customer.Text = "";
            m_cbo_customer.Enabled = true;
            m_date_reservationDate.Value = DateTime.Now;
            m_txt_CMND.Text = "";
        }

        private void m_ckb_DepositCost_CheckedChanged(object sender, EventArgs e)
        {
            if (m_ckb_DepositCost.Checked == true)
            {
                m_txt_DepositCost.Enabled = true;
            }
            else
            {
                m_txt_DepositCost.Enabled = false;
                m_txt_DepositCost.Text = "";
            }

        }

        private void m_cbo_room_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_Reservation.RoomID = m_cbo_room.SelectedValue.ToString();
        }

        private void m_cbo_customer_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_Reservation.CustomerID = int.Parse(m_cbo_customer.SelectedValue.ToString());
            DataTable v_dt = m_ReservationBUS.ViewInfoCustomer(m_Reservation.CustomerID);
            m_txt_CMND.Text = v_dt.Rows[0]["PassportNo"].ToString();
        }
    }
}
